//
//  DispatchViewController.m
//  shanHaiHuiApp
//
//  Created by DS on 2018/3/15.
//  Copyright © 2018年 徐双. All rights reserved.
//

#import "DispatchViewController.h"

#import "LoginViewController.h"

#import "MineAllViewController.h"
#import "MineDispatchViewController.h"
#import "MineViewController.h"


#import "LogOutView.h"


@interface DispatchViewController ()<ZJScrollPageViewDelegate,UITextFieldDelegate>

@property(strong, nonatomic)NSArray<NSString *> *titles;
@property(strong, nonatomic)NSArray<UIViewController<ZJScrollPageViewChildVcDelegate> *> *childVcs;

Strong UITextField * textField;

Strong UIButton *searchBtn;

@end

//网络
@implementation DispatchViewController (NetWorking)
//退出登录接口
- (void)logOutData{
    [self showprogressHUD];
    NSDictionary *parameters = nil;
    [[AFAppDotNetAPIClient sharedClient] POST:@"mloginExit.do" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"code"] intValue] == 0) {
            LoginViewController * vc = [[LoginViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            vc.nameTestField.text = [UserModel sharedInstanced].userID;
            [vc.StoreBtn setTitle:[UserModel sharedInstanced].fMemo forState:UIControlStateNormal];
            [self.navigationController pushViewController:vc animated:YES];
            [UserModel sharedInstanced].token = @"";
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}
@end

@implementation DispatchViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self NavColor];
    //右侧有两个按钮
    UIButton * logOutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [logOutBtn addTarget:self action:@selector(logOutBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [logOutBtn setImage:[UIImage imageNamed:@"tuichudenglu"] forState:UIControlStateNormal];
    
    [logOutBtn sizeToFit];
    UIBarButtonItem *logOutBtnItem = [[UIBarButtonItem alloc] initWithCustomView:logOutBtn];
    
    
    UIBarButtonItem *fixedSpaceBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    fixedSpaceBarButtonItem.width = 22;
    
    
    self.searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.searchBtn addTarget:self action:@selector(searchBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.searchBtn setImage:[UIImage imageNamed:@"sousuo"] forState:UIControlStateNormal];
    [self.searchBtn sizeToFit];
    UIBarButtonItem *searchBtnItem = [[UIBarButtonItem alloc] initWithCustomView:self.searchBtn];
    
    self.navigationItem.rightBarButtonItems  = @[logOutBtnItem,fixedSpaceBarButtonItem,searchBtnItem];
}

// 导航颜色
- (void)NavColor{
    
    UINavigationBar * bar = self.navigationController.navigationBar;
    UIImage *bgImage = [UIImage imageWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64) WihtColor:getColor(navColor) alphe:1.0];
    [bar setBackgroundImage:bgImage forBarMetrics:UIBarMetricsDefault];
    
    
    [bar setShadowImage:[UIImage imageWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 0.1) WihtColor:[UIColor blackColor] alphe:1.0]];
    
}

//输入框
- (UITextField *)textField{
    if (!_textField) {
        _textField  = [[UITextField alloc]initWithFrame:CGRectMake(height20, 26, SCREEN_WIDTH - 100 / WIDTH_5S_SCALE, 30)];
        _textField.font = DEF_FontSize_14;
        _textField.placeholder = @"请录入商品编号、商品名称、商品产地";
        _textField.layer.masksToBounds = YES;
        _textField.layer.cornerRadius = 5;
        _textField.returnKeyType = UIReturnKeySearch;
        _textField.leftView = ({
            UIImageView *leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 20)];
            leftImageView.contentMode = UIViewContentModeScaleAspectFit;
            leftImageView.image = [UIImage imageNamed:@"sousuo_icon"];
            leftImageView;
        });
        _textField.leftViewMode = UITextFieldViewModeAlways;
        _textField.backgroundColor = getColor(@"F0F0F0");
        _textField.delegate = self;
        _textField.tintColor = [UIColor redColor];
        _textField.clearButtonMode = UITextFieldViewModeAlways;
        
        //去调用上面的代码，给这个UITextField的对象添加UIToolbar
        _textField.inputAccessoryView = [self addToolbar];
        
        [_textField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingDidEnd];
        
        
    }
    return _textField;
}


#pragma Action --

- (void)textChange{
    
    [self.textField resignFirstResponder];
    
    [DefaultInstance sharedInstance].info = self.textField.text;
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)aTextfield {
    
    
    [aTextfield resignFirstResponder];//关闭键盘
    
    
    NSDictionary *dataDic = [NSDictionary dictionaryWithObject:aTextfield.text forKey:@"info"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"infoNotification" object:nil userInfo:dataDic];

    return YES;
}



- (void)logOutBtnClick:(UIButton *)sender{
    
    LogOutView * logoutView = [[LogOutView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT ) selectedHandle:^(NSString * str) {
        
    }];
    
    
    
    [logoutView show];
    
    ws(logoutView)
    logoutView.getLogoutClick = ^(LogOutView * _Nonnull view) {
        
    [weaklogoutView dismiss];
        
        [self logOutData];
        
    };
    

}

- (void)searchBtn:(UIButton *)sender{
    
    self.navigationItem.titleView = self.textField;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // iOS7顶部屏幕适配
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]){
        
        self.edgesForExtendedLayout = UIRectEdgeAll;
    }
    
    //必要的设置, 如果没有设置可能导致内容显示不正常
    self.automaticallyAdjustsScrollViewInsets = NO;
    ZJSegmentStyle *style = [[ZJSegmentStyle alloc] init];
    //显示滚动条
    style.showLine = YES;
    // 颜色渐变
    style.gradualChangeTitleColor = YES;
    style.scrollTitle = YES;
    style.autoAdjustTitlesWidth = YES;
    style.adjustCoverOrLineWidth = YES;
    style.selectedTitleColor = getColor(navColor);
    style.scrollLineColor = getColor(navColor);
    style.segmentHeight = 42;
    self.titles = @[@"全部订单",
                    @"未完成",
                    @"已完成",
                    ];
    self.childVcs = [self setupChildVc];
    
    // 初始化
    ZJScrollPageView *scrollPageView = [[ZJScrollPageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - height64 ) segmentStyle:style titles:self.titles parentViewController:self delegate:self];
    [scrollPageView setSelectedIndex:self.index animated:YES];
    scrollPageView.contentView.scrollView.scrollEnabled = YES;
    [self.view addSubview:scrollPageView];

}

#pragma mark ----------------ZJScrollPageViewDelegate---------------------
- (NSInteger)numberOfChildViewControllers {
    return self.titles.count;
}



- (NSArray *)setupChildVc {
    
    MineAllViewController * vc1 = [[MineAllViewController alloc] init];
  
    MineDispatchViewController * vc2 = [[MineDispatchViewController alloc] init];
 
    MineViewController * vc3 = [[MineViewController alloc] init];
  
    
    NSArray *childVcs = [NSArray arrayWithObjects:vc1,vc2,vc3, nil];
    return childVcs;
}

- (UIViewController<ZJScrollPageViewChildVcDelegate> *)childViewController:(UIViewController<ZJScrollPageViewChildVcDelegate> *)reuseViewController forIndex:(NSInteger)index {
    
    UIViewController<ZJScrollPageViewChildVcDelegate> *childVc = reuseViewController;
    
    if (!childVc) {
        childVc = self.childVcs[index];
    }
    
    return childVc;
}


- (UIToolbar *)addToolbar
{
    
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(numberFieldCancle)];
    
    
    toolbar.items = @[space, bar];
    
    return toolbar;
}

- (void)numberFieldCancle{
    
    [self.textField resignFirstResponder];
    
}




@end
